package net.mgsx.product.t2m.modisco.ui;
import org.eclipse.gmt.modisco.infra.query.core.exception.ModelQueryExecutionException;
import org.eclipse.gmt.modisco.infra.query.core.java.IJavaModelQuery;
import org.eclipse.gmt.modisco.infra.query.core.java.ParameterValueList;
import org.eclipse.gmt.modisco.java.ASTNode;
import org.eclipse.gmt.modisco.java.NamedElement;

public class FullQualifiedName implements IJavaModelQuery<ASTNode, String> {
	public String evaluate(final ASTNode context, final ParameterValueList parameterValues)
			throws ModelQueryExecutionException {
		return evaluateRecursive(context);
	}
	
	private String evaluateRecursive(ASTNode node){
		String result = "";
		if(node != null){
			final String name;
			final String separator = ".";
			if(node instanceof NamedElement){
				if(((NamedElement)node).getName() != null){
					name = ((NamedElement)node).getName();
				}else{
					name = node.eClass().getName();
				}
			}else{
				name = node.eClass().getName();
			}
			
			final String parentName;
			if(node.eContainer() instanceof ASTNode){
				parentName = evaluateRecursive((ASTNode)node.eContainer());
			}else{
				parentName = null;
			}
			if(parentName != null){
				result += parentName + separator;
			}
			result += name;
		}
		return result;
	}
}
